# -*- coding: utf-8 -*-
"""
Created on Tue Sep 03 13:15:34 2013

@author: Maxim
"""

from PyQt4 import QtGui, uic
import sys
import win32com.client
from numpy import zeros, dot

class SplineWidget(QtGui.QWidget):
    def __init__(self):
        app = QtGui.QApplication(sys.argv)
        self.ui = uic.loadUi('spline_creator_gui.ui')
        self.catia = win32com.client.Dispatch('catia.application')
        self.ui.show()
        self._init_buttons()
        self._dfltNumPts = 100
        sys.exit(app.exec_())

    def _init_buttons(self):
        self.ui.pushButton.clicked.connect(self.new_part)
        self.ui.pushButton_2.clicked.connect(self.active_document)
        self.ui.pushButton_3.clicked.connect(self.create)

    def new_part(self):
        self.partDocument1 = self.catia.Documents.Add("Part")
        self.active_document()

    def active_document(self):
        self.partDocument1 = self.catia.ActiveDocument
        self.part = self.partDocument1.Part
        self.hSF = self.part.HybridShapeFactory
        self.hBodies = self.part.HybridBodies
        self.hBody = self.hBodies.Item("Geometrical Set.1")

    def create(self):
        self.process_input()
        self.create_spline(self.coord)

    def process_input(self):
        coordRaw = self._get_coord_from_raw(self.ui.plainTextEdit.toPlainText())
        if self.ui.radioButton.isChecked():
            self.coord = self.rearange_coord(coordRaw)
        else:
            self.coord = coordRaw
        self.scale_coord()

    def _get_coord_from_raw(self,string):
        string = str(string)
        data = zeros([self._dfltNumPts,3])
        for i,line in enumerate(string.splitlines()):
            seg = line.split()
            for j,val in enumerate(seg):
                data[i,j] = float(val)
        return data[:i+1]
    
    def rearange_coord(self,coord):
        orient = str(self.ui.comboBox.currentText())
        transform = zeros([3,3])
        if orient=='XY':
            x,y,z = 0,1,2
        elif orient=='XZ':
            x,y,z = 0,2,1
        elif orient=='YX':
            x,y,z = 1,0,2
        elif orient=='YZ':
            x,y,z = 1,2,0
        elif orient=='ZX':
            x,y,z = 2,0,1
        elif orient=='ZY':
            x,y,z = 2,1,0
        transform[0,x]=1
        transform[1,y]=1
        transform[2,z]=1
        return dot(coord,transform)
    
    def scale_coord(self):
        try:
            scale = float(self.ui.lineEdit.text())
        except ValueError:
            scale = 1.0
        self.coord *= scale    

    def create_point(self,point):
        hspc = self.hSF.AddNewPointCoord(point[0],point[1],point[2])
        return self.part.CreateReferenceFromObject(hspc)
    
    def create_spline(self,coord):
        spline = self.hSF.AddNewSpline()
        for point in coord:
            ptRef = self.create_point(point)
            spline.AddPointWithConstraintExplicit(ptRef, None, -1, 1, None, 0)
        self.hBody.AppendHybridShape(spline)
        self.part.update()


if __name__=="__main__":
    SplineWidget()